package com.example.guoxw.mytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private RtspDecoder rtspDecoder = null;
    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());

        surfaceView = (SurfaceView) findViewById(R.id.sv_main);
        Surface surface = surfaceView.getHolder().getSurface();
        rtspDecoder = new RtspDecoder(surface, 0);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    //收到byte[] video格式H264实时流后，调用
    private void onReceiveVideoData(byte[] video) {
        //0x67是SPS的NAL头，0x68是PPS的NAL头
        if (video[4] == 0x67) {
            byte[] tmp = new byte[15];
            //把video中索引0开始的15个数字复制到tmp中索引为0的位置上
            System.arraycopy(video, 0, tmp, 0, 15);
            try {
                rtspDecoder.initial(tmp);
            } catch (Exception e) {
                return;
            }
        } else {
            return;
        }

        if (rtspDecoder != null)
            //设置数据
            rtspDecoder.setVideoData(video);

    }
}
